package cn.itsource.test;

import cn.itsource.utils.HttpClient;
import org.junit.Test;

public class 百度地图API地址解析案例 {
    private static final String ak = "OZCyMqbsc1ntn9ahGHyKWKvxGZ3gcpvS";
    @Test
    public void 根据地址解析获取对应坐标(){
        String api = "http://api.map.baidu.com/geocoding/v3/?address=ADDRESS&output=json&ak=MYAK&callback=showLocation";
        String address = "北京市天安门";
        //下面的地址，是添加了必填参数之后的api地址
        String newApi = api.replace("ADDRESS", address).replace("MYAK", ak);

        //在后台，模拟发送一个http协议请求
        HttpClient httpClient = new HttpClient(newApi);
        httpClient.get();
        String content = httpClient.getContent();
        System.out.println(content);
        //{"status":0,"result":{"location":{"lng":116.40396298757887,"lat":39.91511908708907},"precise":0,"confidence":50,"comprehension":100,"level":"旅游景点"}}

    }

    @Test
    public void 根据2个坐标计算距离(){
        String api = "http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=40.45,116.34&destinations=40.34,116.45&ak="+ak;

        //在后台，模拟发送一个http协议请求
        HttpClient httpClient = new HttpClient(api);
        httpClient.get();
        String content = httpClient.getContent();
        System.out.println(content);
    }
}
